/**
 * 
 * Description: 读取错误提示信息的类，此类可以实现将错误提示中不变部分和变化部分区分开
 * Copyright: Copyright (c) 2008
 * Company:联动优势
 * @author 任水
 * @version 1.0
 * @date Apr 6, 2009
 */
package com.umpay.privi.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtil {
	private static final Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
	private static final String[] files = {"diff_config","checkRule","mapping_regex"};
	private static Properties properties = new Properties();
	static{
		load();
	}
	/**
	 * 
	 * description: 加载配置文件
	 */
	@SuppressWarnings("deprecation")
	private static void load(){
		InputStream in = null;
		for(String file : files){
			URL url = PropertiesUtil.class.getClassLoader().getResource(file + ".properties");		
			try {
				String path = URLDecoder.decode(url.getPath());
				int index = path.indexOf("!/");
				if(index != -1){
					path = path.substring(0, index);
					JarFile earFile = new JarFile(path.substring(path.indexOf("/")));   
					JarEntry jarEntry = earFile.getJarEntry(file + ".properties");   
				    in = earFile.getInputStream(jarEntry);   
				}else in = new FileInputStream(path);
				properties.load(in);
				in.close();
			} catch (Exception e) {
				log.error("加载配置文件" + file + "错误", e);
			}finally{
				if(in != null){
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					in = null;
				}
			}
		}	
	}
	/*
	 * 从消息定义文件中取出code所对应的消息
	 */
	public static String getValue(String key){
		return StringUtil.trim(properties.getProperty(key));
	}
	/**
	 * 
	 * description: 从消息定义文件中取出code所对应的消息, args为占位符的实际值s
	 * @param code
	 * @param args
	 * @return 消息
	 */
	public static String getValue(String key, Object[] args){
		return StringUtil.trim(MessageFormat.format(getValue(key),args));
	}
	public static void main(String[] args){
		String urlStr = PropertiesUtil.getValue("merBankAcc.query.url") + "getMerAccInfo.xml";
		System.out.println(urlStr);
	}
}

